package org.example.netdisk;
import org.example.preparelessons.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {
    @RequestMapping("/user/login")
    public String doLogin(User user, Map<String,Object> map, HttpSession httpSession)
    {
        if (user!=null&& StringUtils.hasText(user.getUsername())&& "123456".equals(user.getPassword()))
        {
            httpSession.setAttribute("loginUser",user);
            System.out.println("LoginController.doLogin 登录成功 用户名："+user.getUsername());
            //防止重复提交，使用重定向
            return "redirect:/main.html";
        }
        else
        {
          map.put("msg","用户名或密码错误"); System.out.println("LoginController.doLogin 登录失败"); return "login";
        }
    }
    @RequestMapping("/user/logout")
    public String doLogout(HttpSession httpSession){
        httpSession.removeAttribute("loginUser");
//跳转界面
        return "login";
    }
}